import 'package:flutter/material.dart';

enum Action {
  // ignore: constant_identifier_names
  OK,Cancel
}

class AlertDialogDemo extends StatefulWidget {
  const AlertDialogDemo({super.key});

  @override
  State<AlertDialogDemo> createState() => _AlertDialogDemoState();
}

class _AlertDialogDemoState extends State<AlertDialogDemo> {

String _choice = 'Nothing';

  Future  _openAlertDialog() async {
  final action = await  showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text('AlertDialog'),
          content: const Text('是否要选择'),
          actions: [
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context, Action.Cancel);
              },
              child: const Text('取消'),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context, Action.OK);
              },
              child: const Text('确认'),
            )
          ],
        );
      },
    );

    switch (action) {
      case Action.Cancel:
        setState(() {
          _choice = '您已取消';
        });
        break;
      case Action.OK:
        setState(() {
          _choice = '您已确认';
        });
        break;
      default:
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AlertDialogDemo'),
        elevation: 0,
      ),
      body: Container(
        padding: const EdgeInsets.all(10),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('你的选择是$_choice'),
            const SizedBox(height: 20,),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed:_openAlertDialog,
                  child: const Text('Open AlertDialog'),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}